home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dviimp / diext.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  9KB  |  365 lines

  1. /*
  2.     External procedures for use with dviimp.
  3.     Written by H. Trickey, 6/25/85.
  4.     
  5.     Implement the following procedures (as declared in Pascal)
  6.  
  7.     function gfbyte: integer;
  8.     function gftwobytes: integer;
  9.     function gfthreebytes: integer;
  10.     function gfsignedquad: integer;
  11.     function getbyte: integer;
  12.     function signedbyte: integer;
  13.     function gettwobytes: integer;
  14.     function signedpair: integer;
  15.     function getthreebytes: integer;
  16.     function signedtrio: integer;
  17.     function signedquad: integer;
  18.     function curpos(var f:bytefile):integer;
  19.     procedure imbyte(w:integer);
  20.     procedure imhalfword(w:integer);
  21.     procedure setpos(var f:bytefile;n:integer);
  22.     procedure setpaths;
  23.     function testaccess(accessmode:integer; filepath:integer):boolean;
  24.     procedure setusername;
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <strings.h>
  29. #include <pwd.h>
  30. #include "h00vars.h" /* pc runtime structures */
  31. #include "texpaths.h"
  32.  
  33. extern struct iorec dvifile,gffile,imfile; /* pc file structs */
  34. extern int eofdvifile,eofgffile; /* need to be set true when these happens */
  35. extern int curloc; /* needs to be incremented when a dvi byte is read */
  36. extern int curgfloc; /* needs to be incremented when a gf byte is read */
  37. extern int imbyteno; /* needs to be incremented when an im byte is written */
  38.  
  39. /* return unsigned version of next byte in gffile */
  40. int gfbyte()
  41. {
  42.     register int c;
  43.     
  44.     if ((c = getc(gffile.fbuf))==EOF) {
  45.         eofgffile = -1;
  46.         return(0);
  47.         }
  48.     curgfloc++;
  49.     return(c);
  50. }
  51.  
  52. /* return unsigned version of next two bytes in gffile */
  53. int gftwobytes()
  54. {
  55.     register int a,b;
  56.     register FILE *iop = gffile.fbuf;
  57.     a = getc(iop);
  58.     eofgffile = ((b = getc(iop))==EOF) ? -1 : 0;
  59.     curgfloc += 2;
  60.     return((a << 8) | b);
  61. }
  62.  
  63. /* return unsigned version of next three bytes in gffile */
  64. int gfthreebytes()
  65. {
  66.     register int a,b,c;
  67.     register FILE *iop = gffile.fbuf;
  68.     a = getc(iop);
  69.     b = getc(iop);
  70.     eofgffile = ((c = getc(iop))==EOF) ? -1 : 0;
  71.     curgfloc +=3;
  72.     return((a << 16) | (b << 8) | c);
  73. }
  74.  
  75. /* return signed version of next four bytes in gffile */
  76. int gfsignedquad()
  77. {
  78.     register int a,b,c,d;
  79.     register FILE *iop = gffile.fbuf;
  80.     a = getc(iop);
  81.     b = getc(iop);
  82.     c = getc(iop);
  83.     eofgffile = ((d = getc(iop))==EOF) ? -1 : 0;
  84.     curgfloc += 4;
  85.     return((a << 24) | (b << 16) | (c << 8) | d);
  86. }
  87.  
  88. /* return unsigned version of next byte in dvifile */
  89. int getbyte()
  90. {
  91.     register int c;
  92.     
  93.     if ((c = getc(dvifile.fbuf))==EOF) {
  94.         eofdvifile = -1;
  95.         return(0);
  96.         }
  97.     curloc++;
  98.     return(c);
  99. }
  100.  
  101. /* return signed version of next byte in dvifile */
  102. int signedbyte()
  103. {    register int c;
  104.     if ((c = getc(dvifile.fbuf))==EOF) {
  105.         eofdvifile = -1;
  106.         return(0);
  107.         }
  108.     curloc++;
  109.     if (c>127) return(c-256);
  110.     return(c);
  111. }
  112.  
  113. /* return unsigned value of next two bytes (high order first) */
  114. int gettwobytes()
  115. {
  116.     register int a,b;
  117.     register FILE *iop = dvifile.fbuf;
  118.     a = getc(iop);
  119.     eofdvifile = ((b = getc(iop))==EOF) ? -1 : 0;
  120.     curloc += 2;
  121.     return((a << 8) | b);
  122. }
  123.  
  124. /* return signed value of next two bytes (high order first) */
  125. int signedpair()
  126. {
  127.     register int a,b;
  128.     register FILE *iop = dvifile.fbuf;
  129.     if ( (a = getc(iop)) > 127) a -= 256; /* sign extend */
  130.     eofdvifile = ((b = getc(iop))==EOF) ? -1 : 0;
  131.     curloc += 2;
  132.     return((a << 8) | b);
  133. }
  134.  
  135. /* return unsigned value of next three bytes */
  136. int getthreebytes()
  137. {
  138.     register int a,b,c;
  139.     register FILE *iop = dvifile.fbuf;
  140.     a = getc(iop);
  141.     b = getc(iop);
  142.     eofdvifile = ((c = getc(iop))==EOF) ? -1 : 0;
  143.     curloc +=3;
  144.     return((a << 16) | (b << 8) | c);
  145. }
  146.  
  147. /* return signed value of next three bytes */
  148. int signedtrio()
  149. {
  150.     register int a,b,c;
  151.     register FILE *iop = dvifile.fbuf;
  152.     if ( (a = getc(iop)) > 127) a -= 256;
  153.     b = getc(iop);
  154.     eofdvifile = ((c = getc(iop))==EOF) ? -1 : 0;
  155.     curloc +=3;
  156.     return((a << 16) | (b << 8) | c);
  157. }
  158.  
  159. /* return value of next four bytes */
  160. int signedquad()
  161. {
  162.     register int a,b,c,d;
  163.     register FILE *iop = dvifile.fbuf;
  164.     a = getc(iop);
  165.     b = getc(iop);
  166.     c = getc(iop);
  167.     eofdvifile = ((d = getc(iop))==EOF) ? -1 : 0;
  168.     curloc += 4;
  169.     return((a << 24) | (b << 16) | (c << 8) | d);
  170. }
  171.  
  172. /* put the low order byte of w on imfile */
  173. imbyte(w)
  174.     int w;
  175. {
  176.     putc(w, imfile.fbuf);
  177.     imbyteno++;
  178. }
  179.  
  180. /* put the two low order bytes of w on imfile, high order first */
  181. imhalfword(w)
  182.     int w;
  183. {
  184.     putc(w >> 8, imfile.fbuf);
  185.     putc(w, imfile.fbuf);
  186.     imbyteno += 2;
  187. }
  188.  
  189. /* seek to byte n of file f; maintain eof's if f is dvifile or gffile */
  190. setpos(f,n)
  191.     register struct iorec *f;
  192.     int n;
  193. {
  194.     if (n>=0) {
  195.         fseek(f->fbuf,(long) n,0); 
  196.         if (f==&dvifile) eofdvifile = 0;
  197.         else if (f==&gffile) eofgffile =0;
  198.         }
  199.     else {
  200.         fseek(f->fbuf, (long) n, 2);
  201.         if (f==&dvifile) eofdvifile = -1;
  202.         else if (f==&gffile) eofgffile = -1;
  203.         }
  204. }
  205.  
  206. /* return current byte offset in file f */
  207. int curpos(f)
  208.     struct iorec *f;
  209. {
  210.     return((int) ftell(f->fbuf));
  211. }
  212.  
  213. char *fontpath;
  214.  
  215. char *getenv();
  216.  
  217. /*
  218.  * setpaths is called to set up the pointer fontpath
  219.  * as follows:  if the user's environment has a value for TEXFONTS
  220.  * then use it;  otherwise, use defaultfontpath.
  221.  */
  222. setpaths()
  223. {
  224.     register char *envpath;
  225.     
  226.     if ((envpath = getenv("TEXFONTS")) != NULL)
  227.         fontpath = envpath;
  228.     else
  229.         fontpath = defaultfontpath;
  230. }
  231.  
  232. #define namelength 100   /* should agree with dvitype.ch */
  233. extern char curname[],realnameoffile[]; /* these have size namelength */
  234.  
  235. /*
  236.  *    testaccess(amode,filepath)
  237.  *
  238.  *  Test whether or not the file whose name is in the global curname
  239.  *  can be opened for reading (if mode=READACCESS)
  240.  *  or writing (if mode=WRITEACCESS).
  241.  *
  242.  *  The filepath argument is one of the ...FILEPATH constants defined below.
  243.  *  If the filename given in curname does not begin with '/', we try 
  244.  *  prepending all the ':'-separated areanames in the appropriate path to the
  245.  *  filename until access can be made, if it ever can.
  246.  *
  247.  *  The realnameoffile global array will contain the name that yielded an
  248.  *  access success.
  249.  */
  250.  
  251. #define READACCESS 4
  252. #define WRITEACCESS 2
  253.  
  254. #define NOFILEPATH 0
  255. #define FONTFILEPATH 3
  256.  
  257. bool
  258. testaccess(amode,filepath)
  259.     int amode,filepath;
  260. {
  261.     register bool ok;
  262.     register char *p;
  263.     char *curpathplace;
  264.     int f;
  265.     
  266.     switch(filepath) {
  267.     case NOFILEPATH: curpathplace = NULL; break;
  268.     case FONTFILEPATH: curpathplace = fontpath; break;
  269.     }
  270.     if (curname[0]=='/')    /* file name has absolute path */
  271.     curpathplace = NULL;
  272.     do {
  273.     packrealnameoffile(&curpathplace);
  274.     if (amode==READACCESS)
  275.         /* use system call "access" to see if we could read it */
  276.         if (access(realnameoffile,READACCESS)==0) ok = TRUE;
  277.         else ok = FALSE;
  278.     else {
  279.         /* WRITEACCESS: use creat to see if we could create it, but close
  280.         the file again if we're OK, to let pc open it for real */
  281.         f = creat(realnameoffile,0666);
  282.         if (f>=0) ok = TRUE;
  283.         else ok = FALSE;
  284.         if (ok)
  285.         close(f);
  286.         }
  287.     } while (!ok && curpathplace != NULL);
  288.     if (ok) {  /* pad realnameoffile with blanks, as Pascal wants */
  289.     for (p = realnameoffile; *p != '\0'; p++)
  290.         /* nothing: find end of string */ ;
  291.     while (p < &(realnameoffile[namelength]))
  292.         *p++ = ' ';
  293.     }
  294.     return (ok ? -1 : 0);
  295. }
  296.  
  297. /*
  298.  * packrealnameoffile(cpp) makes realnameoffile contain the directory at *cpp,
  299.  * followed by '/', followed by the characters in curname up until the
  300.  * first blank there, and finally a '\0'.  The cpp pointer is left pointing
  301.  * at the next directory in the path.
  302.  * But: if *cpp == NULL, then we are supposed to use curname as is.
  303.  */
  304. packrealnameoffile(cpp)
  305.     char **cpp;
  306. {
  307.     register char *p,*realname;
  308.     
  309.     realname = realnameoffile;
  310.     if ((p = *cpp)!=NULL) {
  311.     while ((*p != ':') && (*p != '\0')) {
  312.         *realname++ = *p++;
  313.         if (realname == &(realnameoffile[namelength-1]))
  314.         break;
  315.         }
  316.     if (*p == '\0') *cpp = NULL; /* at end of path now */
  317.     else *cpp = p+1; /* else get past ':' */
  318.     *realname++ = '/';  /* separate the area from the name to follow */
  319.     }
  320.     /* now append curname to realname... */
  321.     p = curname;
  322.     while (*p != ' ') {
  323.     if (realname >= &(realnameoffile[namelength-1])) {
  324.         fprintf(stderr,"! Full file name is too long\n");
  325.         break;
  326.         }
  327.     *realname++ = *p++;
  328.     }
  329.     *realname = '\0';
  330. }
  331.  
  332. extern struct passwd * getpwuid();
  333. extern char username[];
  334. extern int lenusername;
  335. /*
  336.  * find out the caller's name, putting it into username[1..namelength]
  337.  * and put the length used into lenusername
  338.  */
  339. setusername()
  340. {
  341.     int i;
  342.     char *nam;
  343.     
  344.     nam = getpwuid(getuid())->pw_name;
  345.     for (i=0; *nam!=0 && *nam!='(' && i<namelength; )
  346.         username[i++] = *nam++;
  347.     lenusername = i;
  348. }
  349.  
  350. extern char hostname[];
  351. extern int lenhostname;
  352. sethostnam()
  353. {
  354.     int i;
  355.     char nam[namelength];
  356.     
  357.     lenhostname = 0;
  358.     if (gethostname(nam,255) != 0) return;
  359.     for (i=0; nam[i]!=0 && nam[i]!='.' && i<namelength; ) {
  360.         hostname[i] = nam[i];
  361.         i++;
  362.         }
  363.     lenhostname = i;
  364. }
  365.